home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / DOPRNT.C < prev    next >
C/C++ Source or Header  |  1992-04-07  |  18KB  |  726 lines

  1. /* This is file DOPRNT.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /*
  8.  * Copyright (c) 1988 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms are permitted provided
  12.  * that: (1) source distributions retain this entire copyright notice and
  13.  * comment, and (2) distributions including binaries display the following
  14.  * acknowledgement:  ``This product includes software developed by the
  15.  * University of California, Berkeley and its contributors'' in the
  16.  * documentation or other materials provided with the distribution and in
  17.  * all advertising materials mentioning features or use of this software.
  18.  * Neither the name of the University nor the names of its contributors may
  19.  * be used to endorse or promote products derived from this software without
  20.  * specific prior written permission.
  21.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  22.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  24.  */
  25.  
  26. #if defined(LIBC_SCCS) && !defined(lint)
  27. static char sccsid[] = "@(#)doprnt.c    5.39 (Berkeley) 6/28/90";
  28. #endif /* LIBC_SCCS and not lint */
  29.  
  30. #define _doprnt ____doprnt
  31. #include <sys/types.h>
  32. #include <varargs.h>
  33. #include <stdio.h>
  34. #include <ctype.h>
  35. #undef _doprnt
  36.  
  37. /* 11-bit exponent (VAX G floating point) is 308 decimal digits */
  38. #define    MAXEXP        308
  39. /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */
  40. #define    MAXFRACT    39
  41.  
  42. #define    DEFPREC        7
  43. #define    DEFLPREC    16
  44.  
  45. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  46.  
  47. #define    PUTC(ch)    (void) putc(ch, fp)
  48.  
  49. #define ARG(basetype) \
  50.     _ulong = flags&LONGINT ? va_arg(argp, long basetype) : \
  51.         flags&SHORTINT ? (short basetype)va_arg(argp, int) : \
  52.         va_arg(argp, int)
  53.  
  54. #if 0
  55. #define    todigit(c)    ((c) - '0')
  56. #define    tochar(n)    ((n) + '0')
  57. #else
  58. int todigit(char c)
  59. {
  60.   if (c<='0') return 0;
  61.   if (c>='9') return 9;
  62.   return c-'0';
  63. }
  64. char tochar(int n)
  65. {
  66.   if (n>=9) return '9';
  67.   if (n<=0) return '0';
  68.   return n+'0';
  69. }
  70. #endif
  71.  
  72. /* have to deal with the negative buffer count kludge */
  73. #define    NEGATIVE_COUNT_KLUDGE
  74.  
  75. #define    LONGINT        0x01        /* long integer */
  76. #define    LONGDBL        0x02        /* long double; unimplemented */
  77. #define    SHORTINT    0x04        /* short integer */
  78. #define    ALT        0x08        /* alternate form */
  79. #define    LADJUST        0x10        /* left adjustment */
  80. #define    ZEROPAD        0x20        /* zero (as opposed to blank) pad */
  81. #define    HEXPREFIX    0x40        /* add 0x or 0X prefix */
  82.  
  83. _doprnt(fmt0, argp, fp)
  84.     const char *fmt0;
  85.     va_list argp;
  86.     FILE *fp;
  87. {
  88.     u_char *fmt;        /* format string */
  89.     int ch;            /* character from fmt */
  90.     int cnt;        /* return value accumulator */
  91.     int n;            /* random handy integer */
  92.     char *t;        /* buffer pointer */
  93.     double _double;        /* double precision arguments %[eEfgG] */
  94.     u_long _ulong;        /* integer arguments %[diouxX] */
  95.     int base;        /* base for [diouxX] conversion */
  96.     int dprec;        /* decimal precision in [diouxX] */
  97.     int fieldsz;        /* field size expanded by sign, etc */
  98.     int flags;        /* flags as above */
  99.     int fpprec;        /* `extra' floating precision in [eEfgG] */
  100.     int prec;        /* precision from format (%.3d), or -1 */
  101.     int realsz;        /* field size expanded by decimal precision */
  102.     int size;        /* size of converted field or string */
  103.     int width;        /* width from format (%8d), or 0 */
  104.     char sign;        /* sign prefix (' ', '+', '-', or \0) */
  105.     char softsign;        /* temporary negative sign for floats */
  106.     char *digs;        /* digits for [diouxX] conversion */
  107.     char buf[BUF];        /* space for %c, %[diouxX], %[eEfgG] */
  108.  
  109.     if (fp->_flag & _IORW) {
  110.         fp->_flag |= _IOWRT;
  111.         fp->_flag &= ~(_IOEOF|_IOREAD);
  112.     }
  113.     if ((fp->_flag & _IOWRT) == 0)
  114.         return (EOF);
  115.  
  116.     fmt = fmt0;
  117.     digs = "0123456789abcdef";
  118.     for (cnt = 0;; ++fmt) {
  119.         n = fp->_cnt;
  120.         for (t = (char *)fp->_ptr; (ch = *fmt) && ch != '%';
  121.              ++cnt, ++fmt)
  122.             if (--n < 0
  123. #ifdef NEGATIVE_COUNT_KLUDGE
  124.                 && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz)
  125. #endif
  126.                 || ch == '\n' && fp->_flag & _IOLBF) {
  127.                 fp->_cnt = n;
  128.                 fp->_ptr = t;
  129.                 (void) _flsbuf((u_char)ch, fp);
  130.                 n = fp->_cnt;
  131.                 t = (char *)fp->_ptr;
  132.             } else
  133.                 *t++ = ch;
  134.         fp->_cnt = n;
  135.         fp->_ptr = t;
  136.         if (!ch)
  137.             return (cnt);
  138.  
  139.         flags = 0; dprec = 0; fpprec = 0; width = 0;
  140.         prec = -1;
  141.         sign = '\0';
  142.  
  143. rflag:        switch (*++fmt) {
  144.         case ' ':
  145.             /*
  146.              * ``If the space and + flags both appear, the space
  147.              * flag will be ignored.''
  148.              *    -- ANSI X3J11
  149.              */
  150.             if (!sign)
  151.                 sign = ' ';
  152.             goto rflag;
  153.         case '#':
  154.             flags |= ALT;
  155.             goto rflag;
  156.         case '*':
  157.             /*
  158.              * ``A negative field width argument is taken as a
  159.              * - flag followed by a  positive field width.''
  160.              *    -- ANSI X3J11
  161.              * They don't exclude field widths read from args.
  162.              */
  163.             if ((width = va_arg(argp, int)) >= 0)
  164.                 goto rflag;
  165.             width = -width;
  166.             /* FALLTHROUGH */
  167.         case '-':
  168.             flags |= LADJUST;
  169.             goto rflag;
  170.         case '+':
  171.             sign = '+';
  172.             goto rflag;
  173.         case '.':
  174.             if (*++fmt == '*')
  175.                 n = va_arg(argp, int);
  176.             else {
  177.                 n = 0;
  178.                 while (isascii(*fmt) && isdigit(*fmt))
  179.                     n = 10 * n + todigit(*fmt++);
  180.                 --fmt;
  181.             }
  182.             prec = n < 0 ? -1 : n;
  183.             goto rflag;
  184.         case '0':
  185.             /*
  186.              * ``Note that 0 is taken as a flag, not as the
  187.              * beginning of a field width.''
  188.              *    -- ANSI X3J11
  189.              */
  190.             flags |= ZEROPAD;
  191.             goto rflag;
  192.         case '1': case '2': case '3': case '4':
  193.         case '5': case '6': case '7': case '8': case '9':
  194.             n = 0;
  195.             do {
  196.                 n = 10 * n + todigit(*fmt);
  197.             } while (isascii(*++fmt) && isdigit(*fmt));
  198.             width = n;
  199.             --fmt;
  200.             goto rflag;
  201.         case 'L':
  202.             flags |= LONGDBL;
  203.             goto rflag;
  204.         case 'h':
  205.             flags |= SHORTINT;
  206.             goto rflag;
  207.         case 'l':
  208.             flags |= LONGINT;
  209.             goto rflag;
  210.         case 'c':
  211.             *(t = buf) = va_arg(argp, int);
  212.             size = 1;
  213.             sign = '\0';
  214.             goto pforw;
  215.         case 'D':
  216.             flags |= LONGINT;
  217.             /*FALLTHROUGH*/
  218.         case 'd':
  219.         case 'i':
  220.             ARG(int);
  221.             if ((long)_ulong < 0) {
  222.                 _ulong = -_ulong;
  223.                 sign = '-';
  224.             }
  225.             base = 10;
  226.             goto number;
  227.         case 'e':
  228.         case 'E':
  229.         case 'f':
  230.         case 'g':
  231.         case 'G':
  232.             _double = va_arg(argp, double);
  233.             /*
  234.              * don't do unrealistic precision; just pad it with
  235.              * zeroes later, so buffer size stays rational.
  236.              */
  237.             if (prec > MAXFRACT) {
  238.                 if (*fmt != 'g' && *fmt != 'G' || (flags&ALT))
  239.                     fpprec = prec - MAXFRACT;
  240.                 prec = MAXFRACT;
  241.             }
  242.             else if (prec == -1)
  243.             {
  244.                 if (flags&LONGINT)
  245.                     prec = DEFLPREC;
  246.                 else
  247.                     prec = DEFPREC;
  248.             }
  249.             /*
  250.              * softsign avoids negative 0 if _double is < 0 and
  251.              * no significant digits will be shown
  252.              */
  253.             if (_double < 0) {
  254.                 softsign = '-';
  255.                 _double = -_double;
  256.             }
  257.             else
  258.                 softsign = 0;
  259.             /*
  260.              * cvt may have to round up past the "start" of the
  261.              * buffer, i.e. ``intf("%.2f", (double)9.999);'';
  262.              * if the first char isn't NULL, it did.
  263.              */
  264.             *buf = NULL;
  265.             size = cvt(_double, prec, flags, &softsign, *fmt, buf,
  266.                 buf + sizeof(buf));
  267.             if (softsign)
  268.                 sign = '-';
  269.             t = *buf ? buf : buf + 1;
  270.             goto pforw;
  271.         case 'n':
  272.             if (flags & LONGINT)
  273.                 *va_arg(argp, long *) = cnt;
  274.             else if (flags & SHORTINT)
  275.                 *va_arg(argp, short *) = cnt;
  276.             else
  277.                 *va_arg(argp, int *) = cnt;
  278.             break;
  279.         case 'O':
  280.             flags |= LONGINT;
  281.             /*FALLTHROUGH*/
  282.         case 'o':
  283.             ARG(unsigned);
  284.             base = 8;
  285.             goto nosign;
  286.         case 'p':
  287.             /*
  288.              * ``The argument shall be a pointer to void.  The
  289.              * value of the pointer is converted to a sequence
  290.              * of printable characters, in an implementation-
  291.              * defined manner.''
  292.              *    -- ANSI X3J11
  293.              */
  294.             /* NOSTRICT */
  295.             _ulong = (u_long)va_arg(argp, void *);
  296.             base = 16;
  297.             goto nosign;
  298.         case 's':
  299.             if (!(t = va_arg(argp, char *)))
  300.                 t = "(null)";
  301.             if (prec >= 0) {
  302.                 /*
  303.                  * can't use strlen; can only look for the
  304.                  * NUL in the first `prec' characters, and
  305.                  * strlen() will go further.
  306.                  */
  307.                 char *p, *memchr();
  308.  
  309.                 if (p = memchr(t, 0, prec)) {
  310.                     size = p - t;
  311.                     if (size > prec)
  312.                         size = prec;
  313.                 } else
  314.                     size = prec;
  315.             } else
  316.                 size = strlen(t);
  317.             sign = '\0';
  318.             goto pforw;
  319.         case 'U':
  320.             flags |= LONGINT;
  321.             /*FALLTHROUGH*/
  322.         case 'u':
  323.             ARG(unsigned);
  324.             base = 10;
  325.             goto nosign;
  326.         case 'X':
  327.             digs = "0123456789ABCDEF";
  328.             /* FALLTHROUGH */
  329.         case 'x':
  330.             ARG(unsigned);
  331.             base = 16;
  332.             /* leading 0x/X only if non-zero */
  333.             if (flags & ALT && _ulong != 0)
  334.                 flags |= HEXPREFIX;
  335.  
  336.             /* unsigned conversions */
  337. nosign:            sign = '\0';
  338.             /*
  339.              * ``... diouXx conversions ... if a precision is
  340.              * specified, the 0 flag will be ignored.''
  341.              *    -- ANSI X3J11
  342.              */
  343. number:            if ((dprec = prec) >= 0)
  344.                 flags &= ~ZEROPAD;
  345.  
  346.             /*
  347.              * ``The result of converting a zero value with an
  348.              * explicit precision of zero is no characters.''
  349.              *    -- ANSI X3J11
  350.              */
  351.             t = buf + BUF;
  352.             if (_ulong != 0 || prec != 0) {
  353.                 do {
  354.                     *--t = digs[_ulong % base];
  355.                     _ulong /= base;
  356.                 } while (_ulong);
  357.                 digs = "0123456789abcdef";
  358.                 if (flags & ALT && base == 8 && *t != '0')
  359.                     *--t = '0'; /* octal leading 0 */
  360.             }
  361.             size = buf + BUF - t;
  362.  
  363. pforw:
  364.             /*
  365.              * All reasonable formats wind up here.  At this point,
  366.              * `t' points to a string which (if not flags&LADJUST)
  367.              * should be padded out to `width' places.  If
  368.              * flags&ZEROPAD, it should first be prefixed by any
  369.              * sign or other prefix; otherwise, it should be blank
  370.              * padded before the prefix is emitted.  After any
  371.              * left-hand padding and prefixing, emit zeroes
  372.              * required by a decimal [diouxX] precision, then print
  373.              * the string proper, then emit zeroes required by any
  374.              * leftover floating precision; finally, if LADJUST,
  375.              * pad with blanks.
  376.              */
  377.  
  378.             /*
  379.              * compute actual size, so we know how much to pad
  380.              * fieldsz excludes decimal prec; realsz includes it
  381.              */
  382.             fieldsz = size + fpprec;
  383.             realsz = dprec > fieldsz ? dprec : fieldsz;
  384.             if (sign)
  385.                 realsz++;
  386.             if (flags & HEXPREFIX)
  387.                 realsz += 2;
  388.  
  389.             /* right-adjusting blank padding */
  390.             if ((flags & (LADJUST|ZEROPAD)) == 0 && width)
  391.                 for (n = realsz; n < width; n++)
  392.                     PUTC(' ');
  393.             /* prefix */
  394.             if (sign)
  395.                 PUTC(sign);
  396.             if (flags & HEXPREFIX) {
  397.                 PUTC('0');
  398.                 PUTC((char)*fmt);
  399.             }
  400.             /* right-adjusting zero padding */
  401.             if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
  402.                 for (n = realsz; n < width; n++)
  403.                     PUTC('0');
  404.             /* leading zeroes from decimal precision */
  405.             for (n = fieldsz; n < dprec; n++)
  406.                 PUTC('0');
  407.  
  408.             /* the string or number proper */
  409.             n = size;
  410.             if (fp->_cnt - n >= 0 && (fp->_flag & _IOLBF) == 0) {
  411.                 fp->_cnt -= n;
  412.                 bcopy(t, (char *)fp->_ptr, n);
  413.                 fp->_ptr += n;
  414.             } else
  415.                 while (--n >= 0)
  416.                     PUTC(*t++);
  417.             /* trailing f.p. zeroes */
  418.             while (--fpprec >= 0)
  419.                 PUTC('0');
  420.             /* left-adjusting padding (always blank) */
  421.             if (flags & LADJUST)
  422.                 for (n = realsz; n < width; n++)
  423.                     PUTC(' ');
  424.             /* finally, adjust cnt */
  425.             cnt += width > realsz ? width : realsz;
  426.             break;
  427.         case '\0':    /* "%?" prints ?, unless ? is NULL */
  428.             return (cnt);
  429.         default:
  430.             PUTC((char)*fmt);
  431.             cnt++;
  432.         }
  433.     }
  434.     /* NOTREACHED */
  435. }
  436.  
  437. static
  438. cvt(number, prec, flags, signp, fmtch, startp, endp)
  439.     double number;
  440.     register int prec;
  441.     int flags;
  442.     u_char fmtch;
  443.     char *signp, *startp, *endp;
  444. {
  445.     register char *p, *t;
  446.     register double fract;
  447.     int dotrim, expcnt, gformat;
  448.     double integer, tmp, modf();
  449.     char *exponent(), *round();
  450.  
  451. #ifdef hp300
  452.     if (expcnt = isspecial(number, startp, signp))
  453.         return(expcnt);
  454. #endif
  455.  
  456.     dotrim = expcnt = gformat = 0;
  457.     fract = modf(number, &integer);
  458.  
  459.     /* get an extra slot for rounding. */
  460.     t = ++startp;
  461.  
  462.     /*
  463.      * get integer portion of number; put into the end of the buffer; the
  464.      * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
  465.      */
  466.     for (p = endp - 1; integer; ++expcnt) {
  467.         tmp = modf(integer / 10, &integer);
  468.         *p-- = tochar((int)((tmp + .01) * 10));
  469.     }
  470.     switch(fmtch) {
  471.     case 'f':
  472.         /* reverse integer into beginning of buffer */
  473.         if (expcnt)
  474.             for (; ++p < endp; *t++ = *p);
  475.         else
  476.             *t++ = '0';
  477.         /*
  478.          * if precision required or alternate flag set, add in a
  479.          * decimal point.
  480.          */
  481.         if (prec || flags&ALT)
  482.             *t++ = '.';
  483.         /* if requires more precision and some fraction left */
  484.         if (fract) {
  485.             if (prec)
  486.                 do {
  487.                     fract = modf(fract * 10, &tmp);
  488.                     *t++ = tochar((int)tmp);
  489.                 } while (--prec && fract);
  490.             if (fract)
  491.                 startp = round(fract, (int *)NULL, startp,
  492.                     t - 1, (char)0, signp);
  493.         }
  494.         for (; prec--; *t++ = '0');
  495.         break;
  496.     case 'e':
  497.     case 'E':
  498. eformat:    if (expcnt) {
  499.             *t++ = *++p;
  500.             if (prec || flags&ALT)
  501.                 *t++ = '.';
  502.             /* if requires more precision and some integer left */
  503.             for (; prec && ++p < endp; --prec)
  504.                 *t++ = *p;
  505.             /*
  506.              * if done precision and more of the integer component,
  507.              * round using it; adjust fract so we don't re-round
  508.              * later.
  509.              */
  510.             if (!prec && ++p < endp) {
  511.                 fract = 0;
  512.                 startp = round((double)0, &expcnt, startp,
  513.                     t - 1, *p, signp);
  514.             }
  515.             /* adjust expcnt for digit in front of decimal */
  516.             --expcnt;
  517.         }
  518.         /* until first fractional digit, decrement exponent */
  519.         else if (fract) {
  520.             /* adjust expcnt for digit in front of decimal */
  521.             for (expcnt = -1;; --expcnt) {
  522.                 fract = modf(fract * 10, &tmp);
  523.                 if (tmp)
  524.                     break;
  525.             }
  526.             *t++ = tochar((int)tmp);
  527.             if (prec || flags&ALT)
  528.                 *t++ = '.';
  529.         }
  530.         else {
  531.             *t++ = '0';
  532.             if (prec || flags&ALT)
  533.                 *t++ = '.';
  534.         }
  535.         /* if requires more precision and some fraction left */
  536.         if (fract) {
  537.             if (prec)
  538.                 do {
  539.                     fract = modf(fract * 10, &tmp);
  540.                     *t++ = tochar((int)tmp);
  541.                 } while (--prec && fract);
  542.             if (fract)
  543.                 startp = round(fract, &expcnt, startp,
  544.                     t - 1, (char)0, signp);
  545.         }
  546.         /* if requires more precision */
  547.         for (; prec--; *t++ = '0');
  548.  
  549.         /* unless alternate flag, trim any g/G format trailing 0's */
  550.         if (gformat && !(flags&ALT)) {
  551.             while (t > startp && *--t == '0');
  552.             if (*t == '.')
  553.                 --t;
  554.             ++t;
  555.         }
  556.         t = exponent(t, expcnt, fmtch);
  557.         break;
  558.     case 'g':
  559.     case 'G':
  560.         /* a precision of 0 is treated as a precision of 1. */
  561.         if (!prec)
  562.             ++prec;
  563.         /*
  564.          * ``The style used depends on the value converted; style e
  565.          * will be used only if the exponent resulting from the
  566.          * conversion is less than -4 or greater than the precision.''
  567.          *    -- ANSI X3J11
  568.          */
  569.         if (expcnt > prec || !expcnt && fract && fract < .0001) {
  570.             /*
  571.              * g/G format counts "significant digits, not digits of
  572.              * precision; for the e/E format, this just causes an
  573.              * off-by-one problem, i.e. g/G considers the digit
  574.              * before the decimal point significant and e/E doesn't
  575.              * count it as precision.
  576.              */
  577.             --prec;
  578.             fmtch -= 2;        /* G->E, g->e */
  579.             gformat = 1;
  580.             goto eformat;
  581.         }
  582.         /*
  583.          * reverse integer into beginning of buffer,
  584.          * note, decrement precision
  585.          */
  586.         if (expcnt)
  587.             for (; ++p < endp; *t++ = *p, --prec);
  588.         else
  589.             *t++ = '0';
  590.         /*
  591.          * if precision required or alternate flag set, add in a
  592.          * decimal point.  If no digits yet, add in leading 0.
  593.          */
  594.         if (prec || flags&ALT) {
  595.             dotrim = 1;
  596.             *t++ = '.';
  597.         }
  598.         else
  599.             dotrim = 0;
  600.         /* if requires more precision and some fraction left */
  601.         if (fract) {
  602.             if (prec) {
  603.                 do {
  604.                     fract = modf(fract * 10, &tmp);
  605.                     *t++ = tochar((int)tmp);
  606.                 } while(!tmp);
  607.                 while (--prec && fract) {
  608.                     fract = modf(fract * 10, &tmp);
  609.                     *t++ = tochar((int)tmp);
  610.                 }
  611.             }
  612.             if (fract)
  613.                 startp = round(fract, (int *)NULL, startp,
  614.                     t - 1, (char)0, signp);
  615.         }
  616.         /* alternate format, adds 0's for precision, else trim 0's */
  617.         if (flags&ALT)
  618.             for (; prec--; *t++ = '0');
  619.         else if (dotrim) {
  620.             while (t > startp && *--t == '0');
  621.             if (*t != '.')
  622.                 ++t;
  623.         }
  624.     }
  625.     return(t - startp);
  626. }
  627.  
  628. static char *
  629. round(fract, exp, start, end, ch, signp)
  630.     double fract;
  631.     int *exp;
  632.     register char *start, *end;
  633.     char ch, *signp;
  634. {
  635.     double tmp;
  636.     double modf();
  637.  
  638.     if (fract)
  639.         (void)modf(fract * 10, &tmp);
  640.     else
  641.         tmp = todigit(ch);
  642.     if (tmp > 4)
  643.         for (;; --end) {
  644.             if (*end == '.')
  645.                 --end;
  646.             if (++*end <= '9')
  647.                 break;
  648.             *end = '0';
  649.             if (end == start) {
  650.                 if (exp) {    /* e/E; increment exponent */
  651.                     *end = '1';
  652.                     ++*exp;
  653.                 }
  654.                 else {        /* f; add extra digit */
  655.                     *--end = '1';
  656.                     --start;
  657.                 }
  658.                 break;
  659.             }
  660.         }
  661.     /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
  662.     else if (*signp == '-')
  663.         for (;; --end) {
  664.             if (*end == '.')
  665.                 --end;
  666.             if (*end != '0')
  667.                 break;
  668.             if (end == start)
  669.                 *signp = 0;
  670.         }
  671.     return(start);
  672. }
  673.  
  674. static char *
  675. exponent(p, exp, fmtch)
  676.     register char *p;
  677.     register int exp;
  678.     u_char fmtch;
  679. {
  680.     register char *t;
  681.     char expbuf[MAXEXP];
  682.  
  683.     *p++ = fmtch;
  684.     if (exp < 0) {
  685.         exp = -exp;
  686.         *p++ = '-';
  687.     }
  688.     else
  689.         *p++ = '+';
  690.     t = expbuf + MAXEXP;
  691.     if (exp > 9) {
  692.         do {
  693.             *--t = tochar(exp % 10);
  694.         } while ((exp /= 10) > 9);
  695.         *--t = tochar(exp);
  696.         for (; t < expbuf + MAXEXP; *p++ = *t++);
  697.     }
  698.     else {
  699.         *p++ = '0';
  700.         *p++ = tochar(exp);
  701.     }
  702.     return(p);
  703. }
  704.  
  705. #ifdef hp300
  706. isspecial(d, bufp, signp)
  707.     double d;
  708.     char *bufp, *signp;
  709. {
  710.     register struct IEEEdp {
  711.         unsigned sign:1;
  712.         unsigned exp:11;
  713.         unsigned manh:20;
  714.         unsigned manl:32;
  715.     } *ip = (struct IEEEdp *)&d;
  716.  
  717.     if (ip->exp != 0x7ff)
  718.         return(0);
  719.     if (ip->manh || ip->manl)
  720.         (void)strcpy(bufp, "NaN");
  721.     else
  722.         (void)strcpy(bufp, "Inf");
  723.     return(3);
  724. }
  725. #endif
  726.